﻿using DotNetCore.MyBlogApi.Services.IServices.SysManage;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace DotNetCore.MyBlogApi.Api.Controllers
{
    [EnableCors("AllowSameDomain")]
    [Produces("application/json")]
    [ApiController]
    [Route("[controller]")]
    public class ArticleController : ControllerBase
    {
        private IArticleManage _articleManage;
        private readonly ILogger<ArticleController> _logger;
        public ArticleController(IArticleManage articleManage, ILogger<ArticleController> logger)
        {
            _logger = logger;
            _articleManage = articleManage;
        }
        //[HttpGet]
        //public IActionResult Get()
        //{
        //    var articles = _articleManage.LoadAll(null);
        //    return new ObjectResult(articles.ToList());
        //    //TODO:获取所有文章
        //}

        [HttpGet("{id}")]
        public IActionResult Get(Guid id)
        {
            var article = _articleManage.Get(o => o.ArticleId == id);
            _logger.LogDebug($"Get article :{id}");
            return new ObjectResult(article);
            //TODO:根据ID获取一条文章
        }

        [HttpGet]
        public IActionResult Query(Guid userId, Guid catagoryId, int pageIndex, int pageSize)
        {
            var query = _articleManage.LoadAll(null);
            //if (userId != null && userId != Guid.Empty) query = query.Where(o => o.SysUserId == userId);
            //if (catagoryId != null && userId != Guid.Empty) query = query.Where(o => o.CateGoryId == catagoryId);
            if (pageIndex >= 0 && pageSize > 0) query = query.Skip(pageIndex * pageSize).Take(pageSize);
            else query = query.Skip(0).Take(10);//如果没有传页码
            var result = query.ToList();
            return new ObjectResult(result);
        }

        [HttpPost]
        public IActionResult Insert([FromBody] Data.Models.Article article)
        {
            if (article == null)
            {
                return BadRequest();
            }
            // TODO：新增操作
            if (article.ArticleId == null)
                article.ArticleId = Guid.NewGuid();
            return new ObjectResult(_articleManage.Save(article));
        }

        [HttpPut]
        public IActionResult Update([FromBody] Data.Models.Article article)
        {
            if (article == null || article.ArticleId == null)
            {
                return BadRequest();
            }
            var articleOrigin = _articleManage.GetArticleById(article.ArticleId);

            // TODO: 更新操作
            return new ObjectResult(_articleManage.Update(articleOrigin));
        }

        [HttpDelete("{id}")]
        public IActionResult Delete(Guid id)
        {
            // TODO: 删除操作
            return new ObjectResult(new { });
            //return new ObjectResult(_articleManage.Delete(o => o.SysUserId == id));
        }
    }
}
